package cn.itcast.order.service;


import cn.itcast.feign.clients.UserClient;
import cn.itcast.feign.pojo.User;
import cn.itcast.order.mapper.OrderMapper;
import cn.itcast.order.pojo.Order;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

@Service
public class OrderService {

    @Autowired
    private OrderMapper orderMapper;
    @Autowired
    private RestTemplate restTemplate;

    @Autowired
    private UserClient userClient;
    public Order queryOrderById(Long orderId) {
        // 1.查询订单
        Order order = orderMapper.findById(orderId);
//        //2、利用RestTemplate发起http请求，查询用户
//        //2.1 URL路径      通过eureka 服务器转发
//            String url = "http://userservice/user/" + order.getUserId();
//        //2.2发送http请求
//        User user = restTemplate.getForObject(url, User.class);

        //2.利用Feign 发起http请求，查询用户，防止url写死
        User user = userClient.findById(order.getUserId());
        //3 封装user到Order
        order.setUser(user);
        // 4.返回
        return order;
    }

}
